# -*- coding: utf-8 -*-
"""
Created on Fri Aug  5 23:44:44 2016

@author: saninkyu
"""

from urllib2 import urlopen
from urllib import quote
from lxml import etree
import time

class GsExtractor(object):
    def __init__ (self):
        self.xslt = ""
        #从文件读取xslt
        def setXsltFromFile(self, XsltFilePath):
            file = open(xsltFrlePath, 'r')
                try:
                    self.xslt = file.read()
                finally:
                    file.close()
                    #通过GooSeeker API接口获得xslt
                    def setXsltFromAPI(self, APIKey, theme, middle = None,bname= None):
                        apiurl="http://www.gooseeker.com/api/getextractor?Key = "+APIKey+"&theme="+quote(theme)
                        if(middle):
                            apiurl = apiurl + "&middle = "+quote(middle)
                            if (bname):
                                apiurl = apiurl + "&middle="+quote(middle)
                                apiconn = urlopen（apiurl)
                                self.xslt = apiconn.read()
                                #返回当前xslt
                                def getXslt(self):
                                    return self.xslt
                                #提取方法，入参是一个HTML DOM对象，返回是提取结果_
                                   def extract(self, html):
                                       xslt_root = etree.XML(self.xslt)
                                       transform = etree.XSLT(self.Xslt_root)
                                       result_tree = transform(html)
                                       return result_tree
                                       
                    